package blog

import (
	"encoding/json"
	"time"

	"gitlab.com/vionysos/go15035-vic/vblog/apps/common"
)

// 构造函数
func NewBlog() *Blog {
	return &Blog{
		&common.Meta{
			CreatedAt: time.Now().Unix(),
		},
		&CreateBlogRequest{
			Tags: map[string]string{},
		},
		&ChangedBlogStatusRequest{},
	}
}

type Blog struct {
	*common.Meta
	*CreateBlogRequest
	*ChangedBlogStatusRequest
}

func (req *Blog) String() string {
	dj, _ := json.MarshalIndent(req, "", "	")
	return string(dj)

}

type CreateBlogRequest struct {
	Title    string            `json:"title" gorm:"column:title" validate:"required"`
	Author   string            `json:"title" gorm:"column:author" validate:"required"`
	Content  string            `json:"content" gorm:"column:content" validate:"required"`
	Summary  string            `json:"summary" gorm:"column:summary"`
	CreateBy string            `json:"create_by" gorm:"column:create_by"`
	Tags     map[string]string `json:"tags" gorm:"column:tags;serializer:json`
}

func (req *CreateBlogRequest) String() string {
	dj, _ := json.MarshalIndent(req, "", "	")
	return string(dj)

}

// 发布才能修改文章状态
// `published_at` int NOT NULL COMMENT `发布时间`
// `status` tinyint NOT NULL COMMENT `文章状态： 草稿/已发布`
type ChangedBlogStatusRequest struct {
	PublishedAt int64  `json:"published_at" gorm:"column:published_at"`
	Status      Status `json:"status" gorm:"column:status"`
}

func (req *ChangedBlogStatusRequest) String() string {
	dj, _ := json.MarshalIndent(req, "", "	")
	return string(dj)

}
